﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ragipo;
using RechercheConcert;
using System.Diagnostics;





namespace RechercheConcert
{
    public partial class FrmStart : Form
    {


        #region Champs

        private Artist selectedArtist = new Artist();
        Artist selectedArtistDetails = new Artist();
        Event selectedEvent = new Event();
        List<Event> lesEvents = new List<Event>();

        private const string MESSAGE_RECHERCHE_ARTISTE = "Recherche en cours, veuillez patienter ...";
        private const string MESSAGE_SAISIE_ARTISTE = "Veuillez saisir un artiste à rechercher";
        private const string MESSAGE_ARTISTE_INCONNU = "Désolé, l'artiste'#valeur#' est inconnu  ...";

        private const string MESSAGE_RECHERCHE_VILLE = "Recherche en cours, veuillez patienter ...";
        private const string MESSAGE_SAISIE_VILLE = "Veuillez saisir une ville à rechercher";
        private const string MESSAGE_VILLE_INCONNUE = "Désolé, la ville '#valeur#' est inconnue  ...";


        private const string Meteo = "http://www.meteorama.fr/m%C3%A9t%C3%A9o-#valeur#.html";



        private string message;

        // private bool isConnected;

        #endregion

        #region Constructeurs
        public FrmStart()
        {
            InitializeComponent();
        }


        #endregion

        private void toolStripMenuItemFichier_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Etes-vous sûr de vouloir quitter ?", "ZiK'nCo : question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Application.Exit();
            }
        }

        private void btnRechercher_Click(object sender, EventArgs e)
        {
            if (txtArtisteRecherche.Text == string.Empty)
            {
                erp.SetError(txtArtisteRecherche, MESSAGE_SAISIE_ARTISTE);
            }
            else
            {
                Cursor.Current = Cursors.WaitCursor;
                lblaide.Text = MESSAGE_RECHERCHE_ARTISTE;
                lblaide.Refresh();

                rechercherParArtistes();

                txtArtisteRecherche.Clear();
            }

        }


        #region Méthodes

        private void rechercherParArtistes()
        {
            string recherche = txtArtisteRecherche.Text;
            List<Artist> lesArtists = LastfmApi.getAllArtistByName(recherche);



            if (lesArtists.Count == 0)
            {
                message = MESSAGE_ARTISTE_INCONNU.Replace("#valeur#", recherche);
                lblArtisteRecherche.Text = message;
                erp.SetError(txtArtisteRecherche, message);
            }
            else
            {

                erp.SetError(txtArtisteRecherche, string.Empty);
                listBoxArtiste.DataSource = lesArtists;
                listBoxArtiste.DisplayMember = "name";
                txtArtisteRecherche.Text = string.Empty;
                lblaide.Text = "";

            }
        }



        private void listBoxArtiste_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectedArtist = (Artist)listBoxArtiste.SelectedItem;

            lblNomArtiste.Text = selectedArtist.name;
            lklArtiste.Text = selectedArtist.link;
            lklArtiste.Tag = selectedArtist.link;
            picArtiste.ImageLocation = selectedArtist.picture;

            List<Event> lesEvents = LastfmApi.getEventsByArtist(selectedArtist.name);
            lstEvent.DataSource = lesEvents;
            lstEvent.DisplayMember = "title";
        }

        private void FrmStart_Load(object sender, EventArgs e)
        {

        }

        private void lstEvent_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectedEvent = (Event)lstEvent.SelectedItem;
            lblEvent.Text = selectedEvent.title;
            picEvent.ImageLocation = selectedEvent.picture;
            lblDateEvent.Text = selectedEvent.date;
            selectedEvent = (Event)lstEvent.SelectedItem;
            Location laLocationEvent = selectedEvent.venue.uneLocation;
            lklMeteo.Tag = Meteo.Replace("#valeur#", laLocationEvent.ville);
            lblRue.Text = laLocationEvent.rue;
            lblCodePostal.Text = laLocationEvent.codePostal;
            lblLieu.Text = selectedEvent.venue.name;
            lblVille.Text = laLocationEvent.ville;
            lklLieu.Tag = selectedEvent.venue.link;
            
        }

        private void rechercherParVille()
        {
            string rechercheVille = textBoxLocation.Text;
            List<Event> lesEvents = LastfmApi.getGeoEvents(rechercheVille);
            lblArtisteRecherche.Text = string.Empty;

            if (lesEvents.Count == 0)
            {
                message = MESSAGE_VILLE_INCONNUE.Replace("#valeur#", rechercheVille);
                lblAideLocation.Text = message;
                erp.SetError(textBoxLocation, message);
            }
            else
            {
                erp.SetError(textBoxLocation, string.Empty);
                lstEvent.DataSource = lesEvents;
                lstEvent.DisplayMember = "title";
                textBoxLocation.Text = string.Empty;

                lblAideLocation.Text = "";
            }
        }


        #endregion

        private void buttonRechercherville_Click(object sender, EventArgs e)
        {
            if (textBoxLocation.Text == string.Empty)
            {
                erp.SetError(textBoxLocation, MESSAGE_SAISIE_VILLE);
            }
            else
            {
                Cursor.Current = Cursors.WaitCursor;
                lblAideLocation.Text = MESSAGE_RECHERCHE_VILLE;
                lblAideLocation.Refresh();

                rechercherParVille();

                textBoxLocation.Clear();
            }



        }


        private void lklArtiste_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start(lklArtiste.Tag.ToString());
        }

        private void lklLieu_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start(lklLieu.Tag.ToString());
        }

        private void lklMeteo_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start(lklMeteo.Tag.ToString());
        }
        
    }
}
